﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SubSonic;
using ExtAspNet;


namespace TCMSystem.admin
{
    public partial class menu_edit : PageBase
    {
        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        #region Module

        /// <summary>
        /// 当前页面所属的模块
        /// </summary>
        public override ModuleType Module
        {
            get
            {
                return ModuleType.CoreMenu;
            }
        }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            // 权限检查
            if (!CheckPowerEdit())
            {
                CheckPowerFailWithPage();
                return;
            }

            btnClose.OnClientClick = ActiveWindow.GetConfirmHidePostBackReference();

            int id = GetQueryIntValue("id");
            TblMstMenu current = TblMstMenu.FetchByID(id);
            if (current == null)
            {
                // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                return;
            }

            tbxTitle.Text = current.Title;
            //tbxName.Text = current.Name;
            tbxUrl.Text = current.NavigateUrl;
            tbxSortIndex.Text = current.SortIndex.ToString();
            //cbxIsModule.Checked = current.IsModule;
            tbxIcon.Text = current.ImageUrl;
            tbxRemark.Text = current.Remark;

            // 模块名称列表
            ddlModules.DataSource = ModuleTypeHelper.GetAppModules();
            ddlModules.DataBind();
            
            ddlModules.SelectedValue = current.Name;

            // 绑定上级菜单下拉列表
            BindDDL(current);

            
            //// 点击图标的触发器输入框
            //tbxIcon.OnClientTriggerClick = Window1.GetSaveStateReference(tbxIcon.ClientID)
            //       + Window1.GetShowReference("./menu_edit_select_icon.aspx");

            // 预置图标列表
            InitIconList(iconList);

            if (!String.IsNullOrEmpty(current.ImageUrl))
            {
                iconList.SelectedValue = current.ImageUrl;
            }
        }

        public static void InitIconList(ExtAspNet.RadioButtonList iconList)
        {
            string[] icons = new string[] { "BulletYellow", "BulletRed", "BulletPurple", "BulletPink", "BulletOrange", "BulletGreen", "BulletBlue", "BulletBlack" };
            foreach (string icon in icons)
            {
                string value = String.Format("~/res.axd?icon={0}", icon);
                string text = String.Format("<img style=\"vertical-align:bottom;\" src=\"./res.axd?icon={0}\" />&nbsp;{0}", icon);

                iconList.Items.Add(new RadioItem(text, value));
            }
        }

        private void BindDDL(TblMstMenu current)
        {
            //TblMstMenuCollection items = new Select().From<TblMstMenu>()
            //    .OrderAsc(TblMstMenu.SortIndexColumn.ColumnName)
            //    .ExecuteAsCollection<TblMstMenuCollection>();
            List<CustomMenu> mys = CustomMenuHelper.GetCustomMenus();

            ResolveDDL<CustomMenu>(mys, current.Id);

            // 绑定到下拉列表（启用模拟树功能和不可选择项功能）
            ddlParent.EnableSimulateTree = true;
            ddlParent.DataTextField = "Title";
            ddlParent.DataValueField = "Id";
            ddlParent.DataSimulateTreeLevelField = "TreeLevel";
            ddlParent.DataEnableSelectField = "Enabled";
            ddlParent.DataSource = mys;
            ddlParent.DataBind();

            // 选中当前节点的父节点
            ddlParent.SelectedValue = current.ParentMenuId.ToString();
        }

        #endregion

        #region Events

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            int id = GetQueryIntValue("id");
            TblMstMenu item = TblMstMenu.FetchByID(id);
            //item.Name = tbxName.Text.Trim();
            item.Name = ddlModules.SelectedValue;
            item.Title = tbxTitle.Text.Trim();
            item.NavigateUrl = tbxUrl.Text.Trim();
            item.SortIndex = Convert.ToInt32(tbxSortIndex.Text.Trim());
            //item.IsModule = cbxIsModule.Checked;
            item.ImageUrl = tbxIcon.Text;
            item.ParentMenuId = Convert.ToInt32(ddlParent.SelectedValue);
            item.Remark = tbxRemark.Text.Trim();
            item.Save(User.Identity.Name);

            // 更新缓存
            CustomMenuHelper.UpdateCustomMenus();

            //ExtAspNet.Alert.Show("保存成功！", String.Empty, ExtAspNet.Alert.DefaultIcon, ExtAspNet.ActiveWindow.GetHidePostBackReference());
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        #endregion

    }
}
